<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <span>编辑分类</span>
        <el-button style="float: right;" @click="goToList">返回列表</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="上级分类" prop="parentId">
          <treeselect 
            v-model="form.parentId" 
            :options="categoryOptions" 
            :normalizer="normalizer" 
            placeholder="选择上级分类" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类编码" prop="categoryCode">
          <el-input v-model="form.categoryCode" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goToList">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getCategory, updateCategory } from "@/api/cms/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listCategory } from "@/api/cms/category";

export default {
  name: "CategoryEdit",
  components: { Treeselect },
  data() {
    return {
      // 分类树选项
      categoryOptions: [],
      // 表单参数
      form: {
        categoryId: null,
        parentId: 0,
        categoryName: null,
        categoryCode: null,
        orderNum: 0,
        status: "0",
        remark: null
      },
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        categoryCode: [
          { required: true, message: "分类编码不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const categoryId = this.$route.params.categoryId;
    this.getCategoryInfo(categoryId);
    this.getTreeselect();
  },
  methods: {
    /** 获取分类详情 */
    getCategoryInfo(categoryId) {
      getCategory(categoryId).then(response => {
        this.form = response.data;
      });
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      listCategory().then(response => {
        this.categoryOptions = [];
        const data = { categoryId: 0, categoryName: '顶级分类', children: [] };
        data.children = this.handleTree(response.data, "categoryId", "parentId");
        this.categoryOptions.push(data);
      });
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.categoryName,
        children: node.children
      };
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: null,
        parentId: 0,
        categoryName: null,
        categoryCode: null,
        orderNum: 0,
        status: "0",
        remark: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateCategory(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.goToList();
          });
        }
      });
    },
    /** 返回列表 */
    goToList() {
      this.$router.push('/cms/category');
    }
  }
};
</script>